import java.util.*;
import java.util.concurrent.*;

public class CachedThreadPoolTest
{
	public static void main(String[] args) throws Exception
	{
		ExecutorService threadpoolExec = Executors.newCachedThreadPool();
		//ExecutorService threadpoolExec = Executors.newFixedThreadPool(4); // Using FixedThreadPool for this is horrible.You can test that too.
		
		long timeIn = System.currentTimeMillis();
		int count = 1000;
		while(count-- > 0)
			threadpoolExec.submit(new SleeperTask());
			
		threadpoolExec.shutdown();
		threadpoolExec.awaitTermination(1000, TimeUnit.SECONDS);
		long timeOut = System.currentTimeMillis();
		
		System.out.println(timeOut - timeIn);
	}
	
	private static class SleeperTask implements Runnable
	{
		public void run()
		{
			try
			{
				Thread.sleep(1000); // This is to simulate Short-lived and asynchronous tasks. Juz for demo.
				System.out.println("I am done with my sleep and gonna die now, Bye");
			}
			catch(Exception e)
			{
			}
		}
	}
}